package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CourseOrder;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CourseOrderQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICourseOrderService;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/courseorder")
public class CourseOrderController {
    @Autowired
    private ICourseOrderService courseOrderService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ICustomerService customerService;
    @RequestMapping("/list")
    public String potentialList(Model model, @ModelAttribute("qo") CourseOrderQueryObject qo){
        //查询所有的课程共享到页面
        List<SystemDictionaryItem> subjects = systemDictionaryItemService.selectBySystemDictionarySubject();
        model.addAttribute("subjects",subjects);
        //查询所有的客户共享到页面
        List<Customer> customers = customerService.list();
        model.addAttribute("customers",customers);
        PageInfo pageInfo = courseOrderService.query(qo);
        model.addAttribute("result",pageInfo);
        return "/courseorder/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(CourseOrder courseorder){
        if(courseorder.getId() != null){
            courseOrderService.update(courseorder);
        }else {
            courseOrderService.save(courseorder);
        }
        return new JsonResult();
    }

}
